package com.data241210.SpringbootDemo.controller;

import com.data241210.SpringbootDemo.model.Student;
import com.data241210.SpringbootDemo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Objects;

/**
 * Student表的控制器
 */
@Controller
@RequestMapping("/student")
public class StudentController {
//    //依赖注入（控制反转IOC）bean
//    @Autowired
//    private StudentMapper studentMapper;
    @Autowired
    private StudentService studentService;

    //依赖注入redis读写模板
    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/list")
    public String list(Model model){
//        List<Student> studentList=studentMapper.findAll();

        List<Student> studentList=studentService.findAll();
        System.out.println(studentList);
        model.addAttribute("studentList",studentList);
        return "student/list";
    }

    //跳转到添加界面
    @RequestMapping("/add")
    public String add(Model model){
        return "student/add";
    }

    //保存前端网页传递过来的数据
//    @ResponseBody
    @PostMapping("/save")
    public String save(Student student){
        System.out.println(student);
        int res=studentService.save(student);
        if(res!=0){
//            return "添加成功";
            return "redirect:/student/list";
        }else {
//            return "添加失败";
            return "redirect:/error";
        }
//        return "添加成功";
    }

    //删除数据
//    @ResponseBody
    @RequestMapping("/delete")
    public String delete(Integer sno){
//        System.out.println(sno);
        int res=studentService.delete(sno);
        if(res!=0){
//            return "删除成功";
            return "redirect:/student/list";
        }else {
//            return "删除失败";
            return "redirect:/error";
        }
    }

    //跳转编辑界面
    @RequestMapping("/edit")
    public String edit(Model model,Integer sno){
        //根据传递的学号，找到编辑的记录
        Student student=studentService.findBySno(sno);
        System.out.println(student);
        //把要编辑的记录传到前端去修改
        model.addAttribute("student",student);
        return "student/edit";
    }

    //保存编辑数据
    @PostMapping("/update")
//    @ResponseBody
    public String update(Student student){
        int res=studentService.update(student);
        if(res!=0){
//            return "更新成功";
            return "redirect:/student/list";
        }else {
//            return "更新失败";
            return "redirect:/error";
        }
    }
    //错误页
    @RequestMapping("/error")
    public String error(){
        return "error";
    }

    @RequestMapping("/findByName")
    public String findByName(Model model,String sname){
        //按姓名查找
        List<Student> students=studentService.findByName(sname);
        model.addAttribute("studentList",students);
        return "student/list";
    }

    //redis设置Key
    @ResponseBody
    @RequestMapping("/setredisskey")
    public void setRedisKey(){
        ValueOperations operations=redisTemplate.opsForValue();
        operations.set("bookname","java");
    }

    //读取redis
    @RequestMapping("/getrediskey")
    @ResponseBody
    public void getRedisKey(){
        ValueOperations operations=redisTemplate.opsForValue();
        Object object=operations.get("bookname");
        System.out.println(object);
    }
}
